package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.admin.exception.BusinessException;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class ManagerService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private VideoApi videoApi;
    @DubboReference
    private MovementApi movementApi;


    //    用户基本资料
    public PageResult findAllUsers(Integer page, Integer pagesize) {
        Page<UserInfo> pages = userInfoApi.findAll(page,pagesize);
        List<UserInfo> userInfos = pages.getRecords();
        for (UserInfo userInfo : userInfos) {
            if(redisTemplate.hasKey(Constants.FREEZE_USER+userInfo.getId())){
                userInfo.setUserStatus("2");
            }
        }
        return new PageResult(page,pagesize, (int) pages.getTotal(),pages.getRecords());
    }

    //    用户基本资料
    public UserInfo findUserById(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        if(redisTemplate.hasKey(Constants.FREEZE_USER+userInfo.getId())){
            userInfo.setUserStatus("2");
        }
        return userInfo;
    }

    //    视频记录翻页
    public PageResult findAllVideos(Integer page, Integer pagesize, Long uid) {
        PageResult pageResult = videoApi.findAllVideos(page,pagesize,uid);
        List<Video> videoList = pageResult.getItems();
        if(CollUtil.isEmpty(videoList)){
            return new PageResult(page,pagesize,0,null);
        }
        UserInfo userInfo = userInfoApi.findById(uid);
        List<VideoVo> vos = new ArrayList<>();
        for (Video video : videoList) {
            VideoVo vo = VideoVo.init(userInfo, video);
            vos.add(vo);
        }
        pageResult.setItems(vos);
        return pageResult;
    }

    //    动态分页
    public PageResult findAllMovements(Integer page, Integer pagesize, Long uid, Integer state) {
        PageResult pageResult = movementApi.findAllMovements(page,pagesize,uid,state);
        List<Movement> movements = pageResult.getItems();
        if(CollUtil.isEmpty(movements)){
            return new PageResult(page,pagesize,0,null);
        }
        UserInfo userInfo = userInfoApi.findById(uid);
        List<MovementsVo> vos = new ArrayList<>();
        for (Movement movement : movements) {
            MovementsVo vo = MovementsVo.init(userInfo, movement);
            vos.add(vo);
        }
        pageResult.setItems(vos);
        return pageResult;
    }

    //    用户冻结操作
    public Map userFreeze(Map param) {
        String userId = param.get("userId").toString();
        String freezingTime = param.get("freezingTime").toString();
        int day = 0;
        if ("1".equals(freezingTime)) {
            day = 3;
        }else if ("2".equals(freezingTime)) {
            day = 7;
        }else if ("3".equals(freezingTime)) {
            day = -1;
        }
        String json = JSON.toJSONString(param);
        redisTemplate.opsForValue().set(Constants.FREEZE_USER+userId,json,day, TimeUnit.DAYS);
        Map resMap = new HashMap<>();
        resMap.put("message","冻结成功");
        return resMap;
    }

    //    用户解冻操作
    public Map userUnfreeze(Map param) {
        String userId = param.get("userId").toString();
        if(userId==null){
            throw new BusinessException("参数不能为空");
        }
        redisTemplate.delete(Constants.FREEZE_USER + userId);
        Map resMap = new HashMap<>();
        resMap.put("message","解冻成功");
        return resMap;
    }
}
